﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class OrganizationManage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void RefreshPageDataBind()
    {
        GView_ExistedBranch.DataSourceID = "ADSource_PartyBranch";
        GView_ExistedBranch.DataBind();
        DDLst_PartyBranch.DataSourceID = "ADSource_PartyBranch";
        DDLst_PartyBranch.DataBind();
        GView_ExistedGroup.DataSourceID = "ADSource_PartyGroup";
        GView_ExistedGroup.DataBind();

    }
    /// <summary>
    /// 创建党组织中用户点击创建后响应
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Btn_CreateBranch_Click(object sender, EventArgs e)
    {
        //判断用户是否输入党支部名称
        if (0 == TBox_BranchName.Text.Length)
        {
            Lb_CreateBranchMessage.Text = "错误：新建党支部名称不能为空。";
            return;
        }
        //查询在表中是否存在同名的党支部。
        DBOperate myDB = new DBOperate();
        if (myDB.HasRecord("party_PartyGroup",
                            string.Format("(IsPartyBranch = true and PartyBranchName = '{0}')", TBox_BranchName.Text)))
        {
            Lb_CreateBranchMessage.Text = "错误：已存在同名党支部。";
            return;
        }
        myDB = new DBOperate();
        myDB.SetCommandString(string.Format("Insert into party_PartyGroup(PartyBranchName,IsPartyBranch) values('{0}',true)",TBox_BranchName.Text));
        myDB.ExecuteNonReader();
        myDB.CloseDB();
        Lb_CreateBranchMessage.Text = "创建" + TBox_BranchName.Text + "成功";
        RefreshPageDataBind();
    }
    protected void Btn_CreatePartyGroup_Click(object sender, EventArgs e)
    {
        //判断用户是否输入党小组名称
        if (0 == TBox_PartyGroupName.Text.Length)
        {
            Lb_CreatePartyGroupMessage.Text = "错误：新建党小组名不能为空。";
            return;
        }
        //查询在表中是否存在同名的党组织。
        DBOperate myDB = new DBOperate();
        if (myDB.HasRecord("party_PartyGroup",
                            string.Format("(PartyGroupName = '{0}' and PartyBranchName = '{1}')",TBox_PartyGroupName.Text,DDLst_PartyBranch.SelectedValue)))
        {
            Lb_CreatePartyGroupMessage.Text = string.Format("错误：{0}中已存在{1}",DDLst_PartyBranch.SelectedValue,TBox_PartyGroupName.Text);
            return;
        }
        myDB = new DBOperate();
        myDB.SetCommandString(string.Format("Insert into party_PartyGroup(PartyGroupName,PartyBranchName) values('{0}','{1}')", 
                                            TBox_PartyGroupName.Text,
                                            DDLst_PartyBranch.SelectedValue));
        myDB.ExecuteNonReader();
        myDB.CloseDB();
        Lb_CreateBranchMessage.Text = string.Format("{0}创建{1}成功", DDLst_PartyBranch.SelectedValue, TBox_PartyGroupName.Text);
        RefreshPageDataBind();
    }
}
